package com.zlc.big_event.service.impl;

import com.zlc.big_event.mapper.UserMapper;
import com.zlc.big_event.pojo.Result;
import com.zlc.big_event.pojo.User;
import com.zlc.big_event.service.UserService;
import com.zlc.big_event.utils.Md5Util;
import com.zlc.big_event.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 根据用户名查找用户信息
     *
     * @param username 用户名
     * @return 用户信息对象，如果找不到则返回null
     */
    @Override
    public User findByUsername(String username) {
        User u = userMapper.findByUsername(username);
        return u;
    }

    /**
     * 注册用户
     *
     * @param username 用户名
     * @param password 密码
     */
    @Override
    public void register(String username, String password) {
        // 加密密码以增强安全性
        String md5String = Md5Util.getMD5String(password);

        // 在数据库中保存用户名和加密后的密码
        userMapper.add(username, md5String);
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public void updateAvatar(String avatar) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");


        userMapper.updateAvatar(avatar, id);
    }

    @Override
    public void updatePwd(String newPwd) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updatePwd(Md5Util.getMD5String(newPwd), id);
    }


}
